<%@ page import="gfo.Match" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="layout" content="main"/>
    <g:set var="entityName" value="${message(code: 'match.label', default: 'Match')}"/>
    <title><g:message code="default.edit.label" args="[entityName]"/></title>
</head>

<body>
<div class="nav">
</div>

<div class="body">
    <h1><g:message code="default.edit.label" args="[entityName]"/>
        <span class="menuButton"><g:link class="create" action="create">New Match</g:link></span></h1>
    <g:if test="${flash.message}">
        <div class="message">${flash.message}</div>
    </g:if>
    <g:hasErrors bean="${matchInstance}">
        <div class="errors">
            <g:renderErrors bean="${matchInstance}" as="list"/>
        </div>
    </g:hasErrors>
    <g:form method="post">
        <g:hiddenField name="id" value="${matchInstance?.id}"/>
        <g:hiddenField name="version" value="${matchInstance?.version}"/>

        <!--Similar to Match:create.gsp, this view does not distinguish between information pertinent to
        the Match and to the Matchship. That burden is placed on the MatchController, under the 'update'
        action. Match:create.gsp and edit.gsp are the one-stop-shop for tinkering with a match, rather than
        making the user create Matchships and Matches separetely.-->
        <div class="dialog">
            <table>
                <tbody>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="tournament">Tournament</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: matchInstance, field: 'tournament', 'errors')}">
                        <g:select name="tournament.id" from="${gfo.Tournament.list()}" optionKey="id"
                                  value="${matchInstance?.tournament?.id}"/>
                    </td>
                </tr>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="winner">Winner:</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: matchInstance, field: 'winner', 'errors')}">
                        <g:each in="${matchInstance.players}" var="ms">
                            <g:if test="${ms.winner}">
                                <g:select name="winner.id" from="${gfo.Player.list()}" optionKey="id"
                                          value="${ms.player.id}"/><!--THIS RIGHT HURR-->
                            </g:if>
                        </g:each>
                    </td>
                </tr>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="char1">Character (1):</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: matchInstance, field: 'winnerChar', 'errors')}">
                    <g:each in="${matchInstance.players}" var="ms">
                        <g:if test="${ms.winner}">
                            <g:select name="winnerChar.id" from="${gfo.Fighter.list()}" optionKey="id"
                                      value="${ms.character.id}" noSelection="['':'Unknown']"/>
                        </g:if>
                    </g:each>
                    </td>
                </tr>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="loser">Loser:</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: matchInstance, field: 'loser', 'errors')}">
                        <g:each in="${matchInstance.players}" var="ms">
                            <g:if test="${!ms.winner}">
                                <g:select name="loser.id" from="${gfo.Player.list()}" optionKey="id"
                                          value="${ms.player.id}"/>
                            </g:if>
                        </g:each>
                    </td>
                </tr>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="char2">Character (2):</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: matchInstance, field: 'loserChar', 'errors')}">
                    <g:each in="${matchInstance.players}" var="ms">
                        <g:if test="${!ms.winner}">
                            <g:select name="loserChar.id" from="${gfo.Fighter.list()}" optionKey="id"
                                      value="${ms.character.id}" noSelection="['':'Unknown']"/>
                        </g:if>
                    </g:each>
                    </td>
                </tr>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="stage">Stage</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: matchInstance, field: 'stage', 'errors')}">
                        <g:select name="stage.id" from="${gfo.Stage.list()}" optionKey="id"
                                  value="${matchInstance?.stage?.id}" noSelection="['':'Unknown']"/>
                    </td>
                </tr>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="bracket">Bracket:</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: matchInstance, field: 'bracket', 'errors')}">
                        <g:select name="bracket.id" from="${gfo.Bracket.list()}" optionKey="id"
                                  value="${matchInstance?.bracket?.id}"/>
                    </td>
                </tr>

                <tr class="prop">
                    <td valign="top" class="name">
                        <label for="vidURL">Video?</label>
                    </td>
                    <td valign="top" class="value ${hasErrors(bean: matchInstance, field: 'vidURL', 'errors')}">
                        <g:textField name="vidURL" value="${matchInstance?.vidURL}"/>
                    </td>
                </tr>

                </tbody>
            </table>
        </div>

        <div class="buttons">
            <span class="button"><g:actionSubmit class="save" action="update"
                                                 value="${message(code: 'default.button.update.label', default: 'Update')}"/></span>
            <span class="button"><g:actionSubmit class="delete" action="delete"
                                                 value="${message(code: 'default.button.delete.label', default: 'Delete')}"
                                                 onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');"/></span>
        </div>
    </g:form>
</div>
</body>
</html>
